using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using Entities;

namespace DAL
{
    public class SANPHAMDAL
    {
        private SqlConnection cn = null;
        public SANPHAMDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(SANPHAM sp)
        {
            SqlCommand cm = new SqlCommand("San_pham_Insert", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@madn", sp.Madn);
            cm.Parameters.AddWithValue("@madanhm",sp.Madanhm);
            cm.Parameters.AddWithValue("@manv",sp.Manv);
            cm.Parameters.AddWithValue("@makv",sp.Makv);
            cm.Parameters.AddWithValue("@madkien",sp.Madkien);
            cm.Parameters.AddWithValue("@mattsp",sp.Mattsp);
            cm.Parameters.AddWithValue("@tensp",sp.Tensp);
            cm.Parameters.AddWithValue("@luotxem",sp.Luotxem);
            cm.Parameters.AddWithValue("@luotquangcao",sp.Luotquangcao);
            cm.Parameters.AddWithValue("@dohapdan",sp.Dohapdan);
            cm.Parameters.AddWithValue("@soluong",sp.Soluong);
            cm.Parameters.AddWithValue("@giagoc",sp.Giagoc);
            cm.Parameters.AddWithValue("@giaban",sp.Giaban);
            cm.Parameters.AddWithValue("@thoigianbd",sp.Thoigianbd);
            cm.Parameters.AddWithValue("@thoigiankt",sp.Thoigiankt);
            cm.Parameters.AddWithValue("@bando",sp.Bando);
            cm.Parameters.AddWithValue("@tilechietkhau",sp.Tilechietkhau);
            cm.Parameters.AddWithValue("@mota",sp.Mota);
            cm.ExecuteNonQuery();
        }
        public void Update(SANPHAM sp)
        {
            SqlCommand cm = new SqlCommand("San_pham_Update", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@masp", sp.Masp);
            cm.Parameters.AddWithValue("@madn", sp.Madn);
            cm.Parameters.AddWithValue("@madanhm", sp.Madanhm);
            cm.Parameters.AddWithValue("@manv", sp.Manv);
            cm.Parameters.AddWithValue("@makv", sp.Makv);
            cm.Parameters.AddWithValue("@madkien", sp.Madkien);
            cm.Parameters.AddWithValue("@mattsp", sp.Mattsp);
            cm.Parameters.AddWithValue("@tensp", sp.Tensp);
            cm.Parameters.AddWithValue("@luotxem", sp.Luotxem);
            cm.Parameters.AddWithValue("@luotquangcao", sp.Luotquangcao);
            cm.Parameters.AddWithValue("@dohapdan", sp.Dohapdan);
            cm.Parameters.AddWithValue("@soluong", sp.Soluong);
            cm.Parameters.AddWithValue("@giagoc", sp.Giagoc);
            cm.Parameters.AddWithValue("@giaban", sp.Giaban);
            cm.Parameters.AddWithValue("@thoigianbd", sp.Thoigianbd);
            cm.Parameters.AddWithValue("@thoigiankt", sp.Thoigiankt);
            cm.Parameters.AddWithValue("@bando", sp.Bando);
            cm.Parameters.AddWithValue("@tilechietkhau", sp.Tilechietkhau);
            cm.Parameters.AddWithValue("@mota", sp.Mota);
            cm.ExecuteNonQuery();
        }
        public void Delete(int iD)
        {
            SqlCommand cm = new SqlCommand("San_pham_Delete", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@masp", iD);
            cm.ExecuteNonQuery();
        }
        public San_phamCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("San_pham_Get", cn);
            cm.CommandType = CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;

            SqlDataReader dr = cm.ExecuteReader();
            San_phamCollection sanphamCollection = new San_phamCollection();

            while (dr.Read())
            {
                SANPHAM sanpham = new SANPHAM();
                sanpham.Masp = int.Parse(dr.GetValue(0).ToString());
                sanpham.Madn =int.Parse( dr.GetValue(1).ToString());
                sanpham.Madanhm =int.Parse( dr.GetValue(2).ToString());
                sanpham.Manv =int.Parse( dr.GetValue(3).ToString());
                sanpham.Makv = int.Parse(dr.GetValue(4).ToString());
                sanpham.Madkien = int.Parse(dr.GetValue(5).ToString());
                sanpham.Mattsp = int.Parse(dr.GetValue(6).ToString());
                sanpham.Tensp = dr.GetValue(7).ToString();
                sanpham.Luotxem = int.Parse(dr.GetValue(8).ToString());
                sanpham.Luotquangcao = int.Parse(dr.GetValue(9).ToString());
                sanpham.Dohapdan =int.Parse( dr.GetValue(10).ToString());
                sanpham.Soluong = int.Parse(dr.GetValue(11).ToString());
                sanpham.Giagoc = int.Parse(dr.GetValue(12).ToString());
                sanpham.Giaban = int.Parse(dr.GetValue(13).ToString());
                sanpham.Thoigianbd = DateTime.Parse(dr.GetValue(14).ToString());
                sanpham.Thoigiankt =DateTime.Parse( dr.GetValue(15).ToString());
                sanpham.Bando = dr.GetValue(16).ToString();
                sanpham.Tilechietkhau = dr.GetValue(17).ToString();
                sanpham.Mota = dr.GetValue(18).ToString();
                sanphamCollection.Add(sanpham);
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return sanphamCollection;
        }
    }
}

































